﻿using System; 

namespace Plugin
{
    public class TransformMacro : MarshalByRefObject
    {
       
        public float[] Execute(ushort[] aInterpolatedWavelengths, float[] AbsorbanceSpectra, float[] FirstDerivativeSpectra, float[] SecondDerivativeSpectra)
        { 
            float[] correctedSpectra = new float[AbsorbanceSpectra.Length];
            float[] smoothedSpectra = new float[AbsorbanceSpectra.Length];

            float[] savitzkyGolayCoefficients = new float[] {
                                                            -0.0216450206935406f,// center
                                                            -0.018398268148303f, // -1
                                                            -0.00865800771862268f, // -2
                                                            0.00757575873285532f, // -3
                                                            0.030303031206131f, // -4
                                                            0.030303031206131f, // +4
                                                            0.00757575873285532f, // +3
                                                            -0.00865800771862268f, // +2
                                                            -0.018398268148303f // +1
                                                            };
            
            int windowSize     = 5; //Must be an odd number
            int halfWindowSize = (windowSize - 1) / 2;
            float[] smoothingCoefficients = new float[windowSize];
            for (int i = 0; i < windowSize; i += 1)
            {
                smoothingCoefficients[i] = 1f;
            }
                                                        
            //Applies the smoothing filter 
            for (int pixel = 0; pixel < (smoothedSpectra.Length - smoothingCoefficients.Length); ++pixel)
            { 
                for (int windowPixel = 1; windowPixel <= halfWindowSize; ++windowPixel)
                { 
                    smoothedSpectra[pixel + halfWindowSize] += (AbsorbanceSpectra[pixel + (windowPixel - 1)] * smoothingCoefficients[halfWindowSize - (windowPixel - 1)]);
                    smoothedSpectra[pixel + halfWindowSize] += (AbsorbanceSpectra[pixel + windowPixel + halfWindowSize] * smoothingCoefficients[windowSize - windowPixel]);
                } 
                smoothedSpectra[pixel + halfWindowSize] += (AbsorbanceSpectra[pixel + halfWindowSize] * smoothingCoefficients[0]); 
            }
                        
            //Applies the smoothing filter 
            for (int pixel = 0; pixel < (correctedSpectra.Length - savitzkyGolayCoefficients.Length); ++pixel)
            { 
                for (int windowPixel = 1; windowPixel <= 4; ++windowPixel)
                { 
                    correctedSpectra[pixel + 4] += (smoothedSpectra[pixel + (windowPixel - 1)] * savitzkyGolayCoefficients[4 - (windowPixel - 1)]);
                    correctedSpectra[pixel + 4] += (smoothedSpectra[pixel + windowPixel + 4] * savitzkyGolayCoefficients[9 - windowPixel]);
                } 
                correctedSpectra[pixel + 4] += (smoothedSpectra[pixel + 4] * savitzkyGolayCoefficients[0]); 
            } 
            
            for (int pixel = 0; pixel < 50; ++pixel)
            { 
                correctedSpectra[pixel] = 0f;
            }
            
            for (int pixel = correctedSpectra.Length - 50; pixel < correctedSpectra.Length; ++pixel)
            { 
                correctedSpectra[pixel] = 0f;
            }
            
            return correctedSpectra;
        }
    }
}

